//
//  HZJViewController.swift
//  HZJTextView
//
//  Created by HZJ on 17/1/10.
//  Copyright © 2017年 HZJ. All rights reserved.
//

import UIKit

class HZJViewController: UIViewController,HZJImageViewControllerDelegate,UITextViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.randomColor()
        setUpUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setUpUI(){
        //文本框
        view.addSubview(myTextView)
        myTextView.frame = view.frame
        myTextView.backgroundColor = UIColor.white
        myTextView.font = UIFont.systemFont(ofSize: 40, weight: UIFont.Weight(rawValue: 0))
        myTextView.delegate = self
        
        //底部标题栏
        view.addSubview(bottomView)
        bottomView.frame = CGRect(x: 0, y: view.frame.height - 50, width: view.frame.width, height: 50)
        bottomView.backgroundColor = UIColor.gray
        
        //绘图按钮
        bottomView.addSubview(imageBtn)
        imageBtn.backgroundColor = UIColor.randomColor()
        imageBtn.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        imageBtn.center = CGPoint(x: bottomView.frame.width * 0.5, y: bottomView.frame.height * 0.5)
        imageBtn.setTitle("touch", for: UIControl.State())
        imageBtn.addTarget(self, action: #selector(HZJViewController.clickTheImageBtn(_:)), for: UIControl.Event.touchUpInside)
        
        
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            self.myTextView.resignFirstResponder()
        }
        return true
    }
    
    ///点击添加图片按钮
    @objc func clickTheImageBtn(_ btn:UIButton){
        let vc = HZJImageViewController()
        vc.delegate = self
        let nav = UINavigationController(rootViewController: vc)
        nav.modalPresentationStyle = .overFullScreen
        self.present(nav, animated: true, completion: nil)
    }
    //MARK: HZJImageViewControllerDelegate
    func getImage(_ image: UIImage?) {
        if image != nil {
            let str = NSMutableAttributedString(attributedString: self.myTextView.attributedText)
            let ran = NSMakeRange(self.myTextView.selectedRange.location + 1, 0)
            let textAttachment = NSTextAttachment()
            textAttachment.image = image
            let height = self.myTextView.font!.capHeight
            textAttachment.bounds = CGRect(x: 0, y: 0, width: height, height: height)
            let testAttachmentStr = NSAttributedString(attachment: textAttachment)
            str.deleteCharacters(in: self.myTextView.selectedRange)
            str.insert(testAttachmentStr, at: self.myTextView.selectedRange.location)
            str.addAttribute(NSAttributedString.Key.font, value: self.myTextView.font!, range: NSMakeRange(0, str.length))
            myTextView.attributedText = str
            myTextView.selectedRange = ran
        }
    }
    
    
    fileprivate lazy var myTextView = UITextView()
    fileprivate lazy var bottomView = UIView()
    fileprivate lazy var imageBtn = UIButton()
}
